package cn.iocoder.yudao.module.blog.service.posteruser;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.blog.controller.admin.posteruser.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.posteruser.PosterUserDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.blog.convert.posteruser.PosterUserConvert;
import cn.iocoder.yudao.module.blog.dal.mysql.posteruser.PosterUserMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.blog.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 用户海报信息 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class PosterUserServiceImpl implements PosterUserService {

    @Resource
    private PosterUserMapper posterUserMapper;

    @Override
    public Long createPosterUser(PosterUserCreateReqVO createReqVO) {
        PosterUserDO posterUserDO = posterUserMapper.selectByUserId(createReqVO.getUserId());

        if(posterUserDO != null){
            throw exception(POSTER_USER_HAD_EXISTS);
        }

        // 插入
        PosterUserDO posterUser = PosterUserConvert.INSTANCE.convert(createReqVO);
        posterUserMapper.insert(posterUser);
        // 返回
        return posterUser.getId();
    }

    @Override
    public void updatePosterUser(PosterUserUpdateReqVO updateReqVO) {
        // 校验存在
        validatePosterUserExists(updateReqVO.getId());
        // 更新
        PosterUserDO updateObj = PosterUserConvert.INSTANCE.convert(updateReqVO);
        posterUserMapper.updateById(updateObj);
    }

    @Override
    public void deletePosterUser(Long id) {
        // 校验存在
        validatePosterUserExists(id);
        // 删除
        posterUserMapper.deleteById(id);
    }

    private void validatePosterUserExists(Long id) {
        if (posterUserMapper.selectById(id) == null) {
            throw exception(POSTER_USER_NOT_EXISTS);
        }
    }

    @Override
    public PosterUserDO getPosterUser(Long id) {
        return posterUserMapper.selectById(id);
    }

    @Override
    public PosterUserDO getPosterUserByUserId(Long userId){
        return posterUserMapper.selectByUserId(userId);
    }

    @Override
    public List<PosterUserDO> getPosterUserList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return posterUserMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<PosterUserDO> getPosterUserPage(PosterUserPageReqVO pageReqVO) {
        return posterUserMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PosterUserDO> getPosterUserList(PosterUserExportReqVO exportReqVO) {
        return posterUserMapper.selectList(exportReqVO);
    }

}
